package org.litu.app.controller.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.litu.app.service.ISysUserRoleService;
import org.litu.base.log.LtLog;
import org.litu.base.log.LtLogOperation;
import org.litu.base.log.LtLogOperationEnum;
import org.litu.core.base.ApiRes;
import org.litu.core.base.BaseController;
import org.litu.core.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户添加角色控制器
 *
 * @author Administrator
 */
@RestController
@LtLog(module = "用户角色表")
@RequestMapping("/system/sysUserRole")
@Api(value = "用户角色表", tags = {"用户角色表"}, protocols = "http,https")
public class SysUserRoleController extends BaseController {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 用户的角色id列表
     *
     * @param userId 用户Id
     * @return 提交成功，对应的用户角色
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "用户的角色id列表", notes = "根据用户的id，获取角色的id列表")
    @ApiImplicitParam(name = "userId", value = "用户Id", required = true)
    public ApiRes list(@RequestParam(required = true) String userId) {
        if (StringUtils.isBlank(userId)) {
            return ApiRes.error(ResultEnum.ParamError);
        }

        List<String> roleIds = sysUserRoleService.getByUserId(userId);
        return ApiRes.ok(roleIds);
    }

    /**
     * 保存用户的角色
     * 当前没有使用到，如果修改用户角色功能单独开放出来的话，就需要使用到了。
     *
     * @param userId  用户Id
     * @param roleIds 角色Id，逗号隔开
     * @return 保存成功
     */
    @PostMapping(value = "/save")
    @LtLogOperation(operation = LtLogOperationEnum.ADD)
    @ApiOperation(value = "存储用户的角色信息", notes = "存储用户的角色信息，先清空掉用户的角色，重新加入方式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true),
            @ApiImplicitParam(name = "roleIds", value = "角色Id，逗号隔开", defaultValue = "")
    })
    public ApiRes save(@RequestParam(required = true) String userId, @RequestParam(required = true) String roleIds) {
        if (StringUtils.isBlank(userId)) {
            return ApiRes.error(ResultEnum.ParamError, "userId不能为空！");
        }
        boolean res = sysUserRoleService.save(userId, roleIds);
        return res ? ApiRes.ok("保存成功！") : ApiRes.error(ResultEnum.SaveError);
    }
}
